
var oArgs = WScript.Arguments;

if (oArgs.length == 0)
{
   WScript.Echo ("Usage : cscript xslt.js xml xsl transformed_xml");
   WScript.Quit();
}

xmlFile = oArgs(0);// + ".xml";
xslFile = oArgs(1);// + ".xsl";
sTransformedXML = oArgs(2);

main();

function main()
{
/*
  var doc = LoadDOM("test.xml");
  var xsl = LoadDOM("test.xsl");

  var str = doc.transformNode(xsl);
  alert("\ndoc.transformNode:\n"+str);

  var out = MakeDOM(null);
  doc.transformNodeToObject(xsl, out);
  alert("\ndoc.transformNodeToObject:\n"+out.xml);
  */
  TransformXML(xmlFile, xslFile, sTransformedXML);
  
}
/*
TransformXML
*/
function TransformXML(xmlFile, xslFile, sTransformedXML)
{
    var xsl = new ActiveXObject("MSXML2.DOMDocument.6.0");
    var xml = new ActiveXObject("MSXML2.DOMDocument.6.0");
    var xmlResult = new ActiveXObject("MSXML2.DOMDocument.6.0");
    
    //make sure the stylesheet resolves externals
    xsl.resolveExternals = true;
    
    //allow document functions
    xsl.setProperty("AllowDocumentFunction", true);
    xsl.setProperty("AllowXsltScript", true);
    
    xml.validateOnParse = false;
    xml.async = false;
    xml.load(xmlFile);

    if (xml.parseError.errorCode != 0)
    {
       WScript.Echo ("XML Parse Error : " + xml.parseError.reason);
    }

    xsl.async = false;
    xsl.load(xslFile);

    if (xsl.parseError.errorCode != 0)
       WScript.Echo ("XSL Parse Error : " + xsl.parseError.reason);

    //WScript.Echo (xml.transformNode(xsl.documentElement));

    try
    {
       //WScript.Echo (xml.transformNode(xsl.documentElement));
       WScript.Echo ("Transforming...");
       sResult = xml.transformNode(xsl.documentElement);
       WScript.Echo (sResult);
       //xml.transformNodeToObject(xsl.documentElement, xmlResult);
       xmlResult.loadXML(sResult);
       WScript.Echo (xmlResult.parseError.reason);
       xmlResult.save(sTransformedXML);
    }
    catch(err)
    {
       WScript.Echo (xml.errorCode);
       WScript.Echo ("Transformation Error : " + err.number + "*" + err.description);
    }
}
/*
function LoadDOM(file)
{
   var dom;
   try {
     dom = MakeDOM(null);
     dom.load(file);
   }
   catch (e) {
     alert(e.description);
   }
   return dom;
}
*/
/*
function MakeDOM(progID)
{
  if (progID == null) {
    progID = "msxml2.DOMDocument.3.0";
  }

  var dom;
  try {
    dom = new ActiveXObject(progID);
    dom.async = false;
    dom.validateOnParse = false;
    dom.resolveExternals = false;
  }
  catch (e) {
    alert(e.description);
  }
  return dom;
}
*/
/*
function alert(str)
{
  WScript.Echo(str);
}
*/

function copyTemplate()
{
    fso = new ActiveXObject("Scripting.FileSystemObject");
    fso.Copy();
}

/*
var oArgs = WScript.Arguments;

if (oArgs.length == 0)
{
   WScript.Echo ("Usage : cscript xslt.js xml xsl");
   WScript.Quit();
}
xmlFile = oArgs(0) + ".xml";
xslFile = oArgs(1) + ".xsl";

var xsl = new ActiveXObject("MSXML2.DOMDOCUMENT.5.0");
var xml = new ActiveXObject("MSXML2.DOMDocument.5.0");
xml.validateOnParse = false;
xml.async = false;
xml.load(xmlFile);

if (xml.parseError.errorCode != 0)
   WScript.Echo ("XML Parse Error : " + xml.parseError.reason);

xsl.async = false;
xsl.load(xslFile);

if (xsl.parseError.errorCode != 0)
   WScript.Echo ("XSL Parse Error : " + xsl.parseError.reason);

//WScript.Echo (xml.transformNode(xsl.documentElement));

try
{
   WScript.Echo (xml.transformNode(xsl.documentElement));
}
catch(err)
{
   WScript.Echo ("Transformation Error : " + err.number + "*" + err.description);
}


*/
function createZip(sSourceFolder, sTargetZIPFile)
{
var oShellApp, oFSO, iErr, sErrSource, sErrDescription;
oShellApp = new ActiveXObject("Shell.Application");
OFSO = new ActiveXObject("Scripting.FileSystemObject");
}
/******
Function fZip(sSourceFolder,sTargetZIPFile)
'This function will add all of the files in a source folder to a ZIP file
'using Windows' native folder ZIP capability.
'Returns an integer 0 if everything went ok.
Dim oShellApp, oFSO, iErr, sErrSource, sErrDescription
Set oShellApp = CreateObject("Shell.Application")
Set oFSO = CreateObject("Scripting.FileSystemObject")
 'The source folder needs to have a \ on the End
 If Right(sSourceFolder,1) <> "\" Then sSourceFolder = sSourceFolder & "\"
On Error Resume Next 
  'If a target ZIP exists already, delete it
  If oFSO.FileExists(sTargetZIPFile) Then oFSO.DeleteFile sTargetZIPFile,True 
 iErr = Err.Number
On Error GoTo 0
 If iErr <> 0 Then   
  fZip = iErr
  Exit Function
 End If
On Error Resume Next
 'Write the fileheader for a blank zipfile.
 oFSO.OpenTextFile(sTargetZIPFile, 2, True).Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0))
 iErr = Err.Number
On Error GoTo 0
 If iErr <> 0 Then   
  fZip = iErr
  Exit Function
 End If
On Error Resume Next 
 'Start copying files into the zip from the source folder.
 oShellApp.NameSpace(sTargetZIPFile).CopyHere oShellApp.NameSpace(sSourceFolder).Items
 iErr = Err.Number
On Error GoTo 0
 If iErr <> 0 Then   
  fZip = iErr
  Exit Function
 End If
  'Because the copying occurs in a separate process, the script will just continue.  Run a DO...LOOP to prevent the function
  'from exiting until the file is finished zipping.
  Do Until oShellApp.NameSpace(sTargetZIPFile).Items.Count = oShellApp.NameSpace(sSourceFolder).Items.Count
   WScript.Sleep 500
  Loop
fZip = 0
End Function 

*//////